<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Lecturers extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct()
    {
        parent::__construct();
        session_start();
        $this->output->enable_profiler(FALSE);
    }
	    
	public function index()
	{
		/*$data['title'] = "Lecturer Profile";
		$data['slideshow'] = 'site/slideshow';
		$data['main'] = 'site/home';
		$data['right'] = 'site/resources_and_lecturers';
		$this->load->vars($data);
		$this->load->view('site/template');*/
	}
	
	function profile($username)
	{
		$lecturer = $this->MLecturers->getLecturerName($username);
		$data['main'] = 'site/lecturers_profile';// this is using views/lecturers_profile.php
		$data['title'] = $lecturer['username'];
		$data['username'] = $username;	
		$data['right'] = 'site/resources_and_lecturers';
		$data['lecturer'] = $lecturer;
		$captcha_result = '';
		$data['cap_img'] = $this -> _make_captcha();
		$this->load->vars($data);
		$this->load->view('site/template');
	}
	function download(){
		/*$this->form_validation->set_rules('captcha', 'Captcha', 'required');
		if ( $this -> _check_capthca() ) {
			echo '<script type="text/javascript">alert("success");history.back();</script>';
		}
		else{
			echo '<script type="text/javascript">alert("Enter captcha . Please try again!");history.back();</script>';
			//$this->session->set_flashdata('captcha_msg', 'Enter captcha . Please try again!');
			//redirect('lecturers/profile/'.$_POST['username']);
		} */
		if($_POST['file_src'] != null){
			if (file_exists(FCPATH.'images/'.$_POST['username'].'/'.$_POST['file_src'])) {
				$this->load->helper('download');
				$data = file_get_contents(base_url().'images/'.$_POST['username']."/".$_POST['file_src']); // Read the file's contents
				$name = $_POST['file_src'];
				force_download($name, $data);
			}else {
				echo "<script language='javascript'>alert('No resource found!');history.back();</script>";
			}
		}
		else {
			echo "<script language='javascript'>alert('No resource found!');history.back();</script>";
		} 
	}
	
	/**
   * For captcha
   *
   */
   function _make_captcha()
  {
    $this->load->helper('captcha');
    $vals = array(
      'img_path' => './captcha/', // PATH for captcha ( *Must mkdir (htdocs)/captcha )
      'img_url' => base_url().'captcha/', // URL for captcha img
      'img_width' => 145, // width
      'img_height' => 50, // height
      // 'font_path'     => '../system/fonts/2.ttf',
      'expiration' => 7200 , 
      ); 
    // Create captcha
    $cap = create_captcha( $vals ); 
    // Write to DB
    if ( $cap ) {
      $data = array(
        'captcha_id' => '',
        'captcha_time' => $cap['time'],
        'ip_address' => $this -> input -> ip_address(),
        'word' => $cap['word'] , 
        );
      $query = $this -> db -> insert_string( 'captcha', $data );
      $this -> db -> query( $query );
    }else {
      return "Umm captcha not work" ;
    }
    return $cap['image'] ;
  }

  function _check_capthca()
  { 
    // Delete old data ( 2hours)
    $expiration = time()-7200 ;
    $sql = " DELETE FROM captcha WHERE captcha_time < ? ";
    $binds = array($expiration);
    $query = $this->db->query($sql, $binds);
    
    //checking input
    $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
    $binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
    $query = $this->db->query($sql, $binds);
    $row = $query->row();

  if ( $row -> count > 0 )
    {
      return true;
    }
    return false;

  }
  
  /**
   * End of captcha
   */
	
}
